#include "Stretch_Static_lyvm.h"
#include<assert.h>
#include<set>

#define LEN 500
#define RTN_ANG PIE/4.0

//using namespace Ogre;

void Stretch_Static_lyvm::algorithm(){
	vector<DIVA_VM* > vms = _area->getAllVMs();
	vector<DIVA_VM* > linkVms ;
	set<DIVA_VM* > movedVms ;
	vector<DIVA_VM* >::iterator itr;
	vector<DIVA_VM* >::iterator itr2;
	DIVA_VM* p;
	int i, j;
	Ogre::Vector3 iniPosition;
	double count;
	double ang;
	double ang_rtn;

	for(itr=vms.begin(), i=0; itr!=vms.end(); itr++, i++){
		p = (*itr);
		movedVms.insert(p);
		iniPosition = p->getPosition();
		linkVms = _area->getVMsLinkedByVM(p);
		count = linkVms.size();
		ang_rtn = (double)i*RTN_ANG;

		for(itr2=linkVms.begin(); itr2!=linkVms.end(); itr2++){
			p = (*itr2);
			if(movedVms.count(p)>0) 
				count--;
		}

		ang = (2.0)*PIE/count;

		for(itr2=linkVms.begin(), j=0; itr2!=linkVms.end(); itr2++, j++){
			p = (*itr2);
			if(movedVms.count(p)>0) continue;
			Ogre::Real x = iniPosition.x+LEN*cos(j*ang+ang_rtn);
			Ogre::Real y = iniPosition.y+LEN*sin(j*ang+ang_rtn);
			Ogre::Real z = iniPosition.z;
			p->setPosition(x,y,z);
			movedVms.insert(p);
		}

/*		if(i==0) iniPosition = p->getPosition();
		Real x = iniPosition.x+(i%2)*100;
		Real y = iniPosition.y-(i/2)*100;
		Real z = iniPosition.z;
		p->setPosition(x,y,z);
*/
	}
}